from CustomGuiItems.MyDockWidget import MyDockWidget;
from PyQt4 import QtGui, QtCore;

class GridGralMdpParams(QtCore.QObject):    
    def __init__(self, objData):
        super(GridGralMdpParams, self).__init__();
        self.dataModel = objData.dataModel;
        self.objData = objData;
        self.parent = objData.parent;
        self.mainWin = objData.parent.mainWin;
        self.myDockWidget = MyDockWidget("General MDP Parameters", self.parent);
        self.mainWin.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.myDockWidget);
        self.createDockContent();
        self.myDockWidget.setWidget(self.qWidget);
        
    []#Update Grid Data from DataModel
    def updateGrid(self):
        mdpParams = self.dataModel.gralMdpParams;
        self.spbxStates.setValue(mdpParams[0]);
        self.spbxActs.setValue(mdpParams[1]);
        self.spbxTimeEvts.setValue(mdpParams[2]); 
                
    def onBtnApplyClick(self):
        self.spbxActs.setValue(self.dataModel.nActions);
        bStateChange = self.dataModel.nStates != self.spbxStates.value();
        bActChange = self.dataModel.nActions != self.spbxActs.value();
        bTimeChange = self.dataModel.nTimeEvents != self.spbxTimeEvts.value();
        self.dataModel.nStates = self.spbxStates.value();
        self.dataModel.nActions = self.spbxActs.value();
        self.dataModel.nTimeEvents = self.spbxTimeEvts.value();
        self.dataModel.gralMdpParams[0] = self.spbxStates.value();
        self.dataModel.gralMdpParams[1] = self.spbxActs.value();
        self.dataModel.gralMdpParams[2] = self.spbxTimeEvts.value();
        self.dataModel.gralMdpParams[3] = self.spbxDiscF.value();
        if bTimeChange:
            self.dataModel.updateStTraceEvents();
        if bStateChange or bActChange:
            self.dataModel.updateStTrans();
            self.dataModel.updateStDists();
            self.dataModel.updateStInfo();
            self.dataModel.updateRiskActRes();
        self.refreshGrids();
        
    def refreshGrids(self):
        if self.objData.dataView != None:
#             self.objData.dataView.gridStTrans.updateGrid();
#             self.objData.dataView.gridStDists.updateGrid();
#             self.objData.dataView.gridStInfo.updateGrid();
#             self.objData.dataView.gridRiskActRes.updateGrid();
#             self.objData.dataView.gridMdpRiskInfo.updateGrid();
            self.objData.dataView.gridStTraceEvents.updateGrid();
            self.objData.dataView.mazeStInput.updateLbx();        
        
    def createDockContent(self):        
        #Labels
        self.lblStates = QtGui.QLabel("States:", self.mainWin);
        self.lblActs = QtGui.QLabel("Actions:", self.mainWin);
        self.lblTimeEvts = QtGui.QLabel("Time Events:", self.mainWin);
        self.lblDiscF = QtGui.QLabel("Discount Factor:", self.mainWin);
        
        #SpinBoxes
        self.spbxStates = QtGui.QSpinBox(self.mainWin);
        self.spbxActs = QtGui.QSpinBox(self.mainWin);
        self.spbxTimeEvts = QtGui.QSpinBox(self.mainWin);
        self.spbxDiscF = QtGui.QDoubleSpinBox(self.mainWin);
        self.spbxStates.setMaximum(10000);
        self.spbxStates.setMinimum(1);
        self.spbxActs.setMaximum(10);
        self.spbxActs.setMinimum(1);
        self.spbxTimeEvts.setMaximum(10000);
        self.spbxTimeEvts.setMinimum(1);
        self.spbxDiscF.setMaximum(1);
        self.spbxDiscF.setMinimum(0);
        self.spbxStates.setValue(self.dataModel.nActions);
        self.spbxActs.setValue(self.dataModel.nStates);
        self.spbxTimeEvts.setValue(self.dataModel.nTimeEvents);
        self.spbxDiscF.setValue(0);
        self.spbxDiscF.setVisible(False);
        self.lblDiscF.setVisible(False);
        self.spbxStates.setMinimumWidth(50);
        self.spbxActs.setMinimumWidth(50);
        self.spbxTimeEvts.setMinimumWidth(50);
        
        #Push Button
        self.btnApply = QtGui.QPushButton("Apply", self.mainWin);
        self.btnApply.connect(self.btnApply, QtCore.SIGNAL("clicked()"), self.onBtnApplyClick);
#        self.btnApply.setMaximumWidth(60);
        
        self.gridLayout = QtGui.QGridLayout();
#        self.gridLayout.addWidget(self.lblStates, 0,0, QtCore.Qt.AlignRight);
#        self.gridLayout.addWidget(self.spbxStates, 0,1, QtCore.Qt.AlignLeft);
#        self.gridLayout.addWidget(self.lblActs, 1,0, QtCore.Qt.AlignRight);
#        self.gridLayout.addWidget(self.spbxActs, 1,1, QtCore.Qt.AlignLeft);
#        self.gridLayout.addWidget(self.lblTimeEvts, 2,0, QtCore.Qt.AlignRight);
#        self.gridLayout.addWidget(self.spbxTimeEvts, 2,1, QtCore.Qt.AlignLeft);
#        self.gridLayout.addWidget(self.lblDiscF, 3,0, QtCore.Qt.AlignRight);
#        self.gridLayout.addWidget(self.spbxDiscF, 3,1);
#        self.gridLayout.addWidget(self.btnApply, 4,1, QtCore.Qt.AlignLeft);
        self.gridLayout.addWidget(self.lblStates, 0,0);
        self.gridLayout.addWidget(self.spbxStates, 1,0);
        self.gridLayout.addWidget(self.lblActs, 0,1);
        self.gridLayout.addWidget(self.spbxActs, 1,1);
        self.gridLayout.addWidget(self.lblTimeEvts, 0,2);
        self.gridLayout.addWidget(self.spbxTimeEvts, 1,2);
        self.gridLayout.addWidget(self.lblDiscF, 0,3);
        self.gridLayout.addWidget(self.spbxDiscF, 1,3);
        self.gridLayout.addWidget(self.btnApply, 2,2);
        self.gridLayout.setRowStretch(3,1);
        self.qWidget = QtGui.QWidget(self.mainWin);
        self.qWidget.setLayout(self.gridLayout);
        self.updateGrid();
